 $(document).ready(function() {
             //		权限判断
		if($.cookie('ID') == null){
			window.location.href="http://www.duoweish.com/crm/login.html";
		}
//		2，3号权限
		if($.cookie('Right') == 1){
			window.location.href="http://www.duoweish.com/crm/index.html";
		}
		///
		$('.dw_name').text($.cookie('Name'));
		 $('.dw_part').text($.cookie('Department'));
 	
			  var page_num = 0;		
             $(".select2").select2();
              $('#reservation').daterangepicker({
             	format:'YYYY-MM-DD',
             	startDate:'2017-10-10',
             	locale:{
             		format:'YYYY-MM-DD'
             	}
             });
             
             init(1);
             $('.search_btn').click(function(){
             	 
			  Ajax(1)
				
            })
           
            function Ajax(curr){
            	 var Kind_data = 0;
			      var onoff = "True";//客户名or项目名
			      var sea_text =$.trim($('.search_text').val());//搜索框内容
				  var data_rang = $('.time_range').val();//时间范围
				  
				  //时间范围转化
				  var start = data_rang.split(' -')[0];
				  var end = data_rang.split('- ')[1];
				if($('.project_box').is(':checked')){
					Kind_data = 1;
					
				}else if($('.user_box').is(':checked')){
					Kind_data = 2;
				}
				if(data_rang==""){onoff="False";}
				if(curr==1){
					onoff = "False";
				}
            	$.ajax({
				headers:{
				    	Accept:"application/json;charset=utf-8"
				    },
					type:"get",
					url:"http://www.duoweish.com/apis/EventInfo/",
					data:{
                            UserId:$.cookie('ID'),
							Date:onoff,
							DateBegin:start,
							DateEnd:end,
							Kind:Kind_data,
							Content:sea_text,
							page:curr||1,
							rows:8
					},
					success:function(data){
						if(data.length!=0){
							page_num = data[1];
						    tableData(data);
					     	divePage(page_num,curr);
						}else{
							layer.msg('没找到该商家')
						}
						
						}
				})
            	
            }
            function tableData(ret){
               
                    $('#table_body').empty();
                    for(var i = 0;i < ret.length;i++){
                    	for (var j = 0;j<ret[i].length;j++) {
                    		
                    	
                        var tr = `<tr>
                                <td style="line-height:20px;">${ret[i][j][0]}</td>
                                <td style="line-height:20px;">${ret[i][j][1]}</td>
                                <td style="line-height:20px;">${ret[i][j][2]}</td>
                                <td style="line-height:20px;">${ret[i][j][3]}</td>
                                <td style="line-height:20px;"> ${ret[i][j][4]}</td>
                                <td>${ret[i][j][5]} </td>
                                <td index="${ret[i][j][6]}">
                                   <button  class="look_btn">查看</button>
                                </td>
                               
                            </tr>`;
                        $('#table_body').append(tr);
                    }
                }
                   
                   
                   
                   //查看详情
                 $('.look_btn').click(function(){
                 	var infor = $(this).parent().attr("index");
                 	        layer.open({
									        type: 2
									        ,anim:1
									        ,title: false
									        ,closeBtn: 1
									        ,shade: 0.5
									        ,shadeClose:true
									        ,area:'600px'
									        ,id:'my_open'
									        ,scrollbar:false
									        ,btnAlign: 'c'
									        ,moveType: 1 
									        ,content:'http://www.duoweish.com/crm/open_event.html?data_id='+infor
										    ,success: function(index){
										    	setTimeout(function(){
										    		var new_H=$('#my_open').find("iframe").contents().find("#now_event").height()+3;
										    		console.log(new_H)
										    		$("#my_open").find("iframe").css('height',new_H+'px');
										    	},200)
										    	
										    	
										    	     
							          	}
							    	 })			
                 })
                 $('.add_new').click(function(){
                 	          layer.open({
									        type: 2
									        ,anim:1
									        ,title: '新建事件'
									        ,closeBtn: 1
									        ,shade: 0.5
									        ,shadeClose:true
									        ,area:['600px','650px']
									        ,id:'my_open'
									        ,scrollbar:false
									        ,btnAlign: 'c'
									        ,moveType: 1 
									        ,content:'http://www.duoweish.com/crm/new_event.html'
										    ,success: function(index){
										    	
										    	
										    	
										    	     
							          	}
							    	 })
                 })
                 /////////////
                 
      
                    
            }
           function divePage(page_num,curr){
            layui.use(['laypage', 'layer'], function(){
					 var laypage = layui.laypage
			  		,layer = layui.layer;
                     laypage.render({
						    elem: 'demo0'
						    ,count: page_num
						    ,curr:curr
						    ,limit:8
						    ,jump: function(obj, first){
						    	   if(!first){
						                Ajax(obj.curr,obj.curr);
						               }
				        
						      		}
						   		});
					 })		
           }
          
           function init(curr){ 
           	     Ajax(1)
	            divePage(page_num);
           }
          
     
           
})